﻿using HotelManager.Common;
using HotelManager.Extensions;
using HotelManager.Models;
using HotelManager.Service;
using MaterialDesignColors;
using Prism.Commands;
using Prism.Ioc;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls.Primitives;

namespace HotelManager.ViewModels
{
    public class RoomViewModel: NavigationViewModel
    {
        private readonly IDialogHostService dialog;
        private readonly IRegionManager regionManager;
        private readonly ITypeService typeService;
        private readonly IRoomStateService roomStateService;
        private readonly IRoomService roomService;
        public RoomViewModel(IDialogHostService dialog, IContainerProvider provider,IRoomService _roomService) : base(provider)
        {
            SelectedRoomTypeStatus = new RoomTypeData();
            SelectedRoomStateStatus = new RoomStateData();
            RoomTypeLists = new ObservableCollection<RoomTypeData>();
            RoomStateLists = new ObservableCollection<RoomStateData>();
            RoomDetailLists=new ObservableCollection<RoomDetailData>();
            ChangedRoomTypeCommand = new DelegateCommand<RoomTypeData>(ChangeType);
            ChangedRoomStateCommand = new DelegateCommand<RoomStateData>(ChangeState);

            SearchCommand = new DelegateCommand(Search);
            ExecuteCommand = new DelegateCommand<string>(Execute);
            SelectedCommand = new DelegateCommand<RoomDetailData>(Selected);
            DelCommand = new DelegateCommand<RoomDetailData>(Delete);
            regionManager = provider.Resolve<IRegionManager>();
            this.dialog = dialog;
            typeService = provider.Resolve<ITypeService>();
            roomStateService=provider.Resolve<IRoomStateService>();
            roomService = _roomService;
            GetRoomTypeDataAsync();
            GetRoomStateDataAsync();
        }
        public DelegateCommand<RoomDetailData> SelectedCommand { get; private set; }
        public DelegateCommand<RoomDetailData> DelCommand { get; private set; }
        private async void Delete(RoomDetailData obj)
        {
            var dialogResult = await dialog.Question("温馨提示", $"确认删除客房编号：{obj.roomId}?");
            if (dialogResult.Result != ButtonResult.OK) return;
            var deleteResult = await roomService.DeleteAsync(new RoomDetailData() { roomId = obj.roomId});
            if (deleteResult.success)
            {
                var model = RoomDetailLists.FirstOrDefault(o => o.roomId.Equals(obj.roomId));
                if (model != null)
                    RoomDetailLists.Remove(model);
            }
        }
        private async void Selected(RoomDetailData roomDetailData)
        {
            try
            {
                UpdateLoading(true);
                var memoResult = await roomService.GetFirstOrDefaultAsync(roomDetailData.roomId);
                if (memoResult is not null)
                {
                    CurrentDto = new RoomDetailData() { roomId = memoResult.roomId, description = memoResult.description, roomTypeId = memoResult.roomTypeId, roomStateId = memoResult.roomStateId};
                    IsRightDrawerOpen = true;
                    PrevId = memoResult.roomId;
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                UpdateLoading(false);
            }
        }
        private int prevId;

        public int PrevId
        {
            get { return prevId; }
            set { prevId = value; RaisePropertyChanged(); }
        }

        public DelegateCommand<string> ExecuteCommand { get; private set; }
        private void Execute(string Operater)
        {
            switch (Operater)
            {
                case "Add":
                    OpenDraw();
                    break;
                case "Save":
                    Save();
                    break;
            }
        }
        
        private async void Save()
        {
            if (CurrentDto.roomId<=0 || string.IsNullOrWhiteSpace(CurrentDto.description))
                return;
            UpdateLoading(true);
            try
            {
                if (PrevId== CurrentDto.roomId)
                {
                    var updateResult = roomService.Update(new RoomUpdateData() {id= PrevId, roomId = CurrentDto.roomId ,description=CurrentDto.description,roomTypeId=CurrentDto.roomTypeId,roomStateId=CurrentDto.roomStateId});
                    if (updateResult.success)
                    {
                        PrevId = -1;
                        var todo = RoomDetailLists.FirstOrDefault(t => t.roomId == CurrentDto.roomId);
                        if (todo != null)
                        {
                            todo.roomId = CurrentDto.roomId;
                            GetDataAsync(SelectedRoomTypeStatus == null ? 0 : SelectedRoomTypeStatus.RoomTypeId, SelectedRoomStateStatus == null ? 0 : SelectedRoomStateStatus.roomStateId, 0, 0);
                        }
                    }
                    IsRightDrawerOpen = false;
                }
                else
                {
                    var addResult = await roomService.AddAsync(CurrentDto);
                    if (addResult.success)
                    {
                        //RoleLists.Add(addResult.Result);
                        GetDataAsync(SelectedRoomTypeStatus == null ? 0 : SelectedRoomTypeStatus.RoomTypeId, SelectedRoomStateStatus == null ? 0 : SelectedRoomStateStatus.roomStateId, 0, 0);
                        IsRightDrawerOpen = false;
                    }
                }
            }
            catch (Exception ex)
            {

            }
            finally { UpdateLoading(false); }
        }
        private void OpenDraw()
        {
            CurrentDto = new RoomDetailData();
            PrevId = -1;
            IsRightDrawerOpen = true;
        }
        private RoomDetailData currentDto;

        public RoomDetailData CurrentDto
        {
            get { return currentDto; }
            set { currentDto = value; RaisePropertyChanged(); }
        }
        private bool isRightDrawerOpen;

        public bool IsRightDrawerOpen
        {
            get { return isRightDrawerOpen; }
            set { isRightDrawerOpen = value; RaisePropertyChanged(); }
        }
        public DelegateCommand SearchCommand { get; private set; }
        private void Search()
        {
            int typeId = SelectedRoomTypeStatus == null ? 0 : SelectedRoomTypeStatus.RoomTypeId;
            int stateId = SelectedRoomStateStatus == null ? 0 : SelectedRoomStateStatus.roomStateId;
            GetDataAsync(typeId, stateId, 0, 0);
        }
        public ObservableCollection<RoomTypeData> roomTypeLists;
        public ObservableCollection<RoomTypeData> RoomTypeLists
        {
            get { return roomTypeLists; }
            set { roomTypeLists = value; RaisePropertyChanged(); }
        }
        public ObservableCollection<RoomStateData> roomStateLists;
        public ObservableCollection<RoomStateData> RoomStateLists
        {
            get { return roomStateLists; }
            set { roomStateLists = value; RaisePropertyChanged(); }
        }
        public ObservableCollection<RoomDetailData> roomDetailLists;
        public ObservableCollection<RoomDetailData> RoomDetailLists
        {
            get { return roomDetailLists; }
            set { roomDetailLists = value; RaisePropertyChanged(); }
        }
        public DelegateCommand<RoomTypeData> ChangedRoomTypeCommand { get; private set; }
        public DelegateCommand<RoomStateData> ChangedRoomStateCommand { get; private set; }
        private void ChangeType(RoomTypeData typeDatas)
        {
            SelectedRoomTypeStatus = typeDatas;
        }
        private void ChangeState(RoomStateData stateDatas)
        {
            SelectedRoomStateStatus = stateDatas;
        }
        public RoomTypeData selectedRoomTypeStatus;
        public RoomTypeData SelectedRoomTypeStatus
        {
            get { return selectedRoomTypeStatus; }
            set
            {
                selectedRoomTypeStatus = value;
                RaisePropertyChanged();
            }
        }
        public RoomStateData selectedRoomStateStatus;
        public RoomStateData SelectedRoomStateStatus
        {
            get { return selectedRoomStateStatus; }
            set
            {
                selectedRoomStateStatus = value;
                RaisePropertyChanged();
            }
        }
        private int selectedTypeIndex;

        public int SelectedTypeIndex
        {
            get { return selectedTypeIndex; }
            set 
            { 
                selectedTypeIndex = value;
                RaisePropertyChanged();
            }
        }
        private int selectedStateIndex;

        public int SelectedStateIndex
        {
            get { return selectedStateIndex; }
            set 
            { 
                selectedStateIndex = value;
                RaisePropertyChanged();
            }
        }
        private async void GetRoomTypeDataAsync()
        {
            var roomTypeListResult = await typeService.GetAllAsync();
            if (roomTypeListResult.Any())
            {
                RoomTypeLists.Clear();
                RoomTypeLists.Add(new RoomTypeData() { RoomTypeId = 0, RoomTypeName = "请选择角色" });
                foreach (var item in roomTypeListResult)
                {
                    RoomTypeLists.Add(item);
                }
            }

        }
        private async void GetRoomStateDataAsync()
        {
            var roomStateListResult = await roomStateService.GetAllAsync();
            if (roomStateListResult.Any())
            {
                RoomStateLists.Clear();
                RoomStateLists.Add(new RoomStateData() { roomStateId = 0, roomStateName = "请选择角色" });
                foreach (var item in roomStateListResult)
                {
                    RoomStateLists.Add(item);
                }
            }

        }
        private void GetDataAsync(int roomTypeId,int roomStateId, int pageIndex, int pageSize)
        {
            UpdateLoading(true);
            var roomListResult = roomService.GetAll(roomTypeId, roomStateId, pageIndex, pageSize);
            if (roomListResult is not null && roomListResult.count > 0)
            {
                RoomDetailLists.Clear();
                foreach (var item in roomListResult.data)
                {
                    
                    RoomDetailLists.Add(item);
                }
            }
            UpdateLoading(false);
        }
        public override void OnNavigatedTo(NavigationContext navigationContext)
        {
            base.OnNavigatedTo(navigationContext);

            //获取role
            
            GetDataAsync(selectedRoomTypeStatus.RoomTypeId, selectedRoomStateStatus.roomStateId, 0,0);
        }
    }
}
